package org.apache.dolphinscheduler.api.service.impl;

import java.util.List;

import org.apache.dolphinscheduler.api.service.TypeService;
import org.apache.dolphinscheduler.dao.entity.Type;
import org.apache.dolphinscheduler.dao.mapper.TypeMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

	public List<Type> queryTypeList(Integer type) {
		QueryWrapper<Type> qw = new QueryWrapper<Type>();
		qw.eq("type", type);
		qw.eq("pid", -1);
		qw.orderByAsc("sort");
		List<Type> typeList = list(qw);
		if(typeList != null && typeList.size() > 0) {
			for(Type t : typeList) {
				t.setChildList(queryTypeListByPid(t.getId()));
			}
		}
		return typeList;
	}
	
	public List<Type> queryTypeListByPid(Integer pid) {
		QueryWrapper<Type> qw = new QueryWrapper<Type>();
		qw.eq("pid", pid);
		qw.orderByAsc("sort");
		return list(qw);
	}
	
	public Type getType(Integer id) {
		Type t = getById(id);
		if(t != null) {
			if(t.getPid() != -1) {
				Type ptype = getById(t.getPid());
				if(ptype != null) {
					t.setPname(ptype.getName());
				}
			}
		}
		return t;
	}
}
